package org.nearby.note;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

import org.nearby.note.utils.common;

/*
 * 局域网聊天工具客户端
 */
public class ChatClient extends JFrame {
    private static final int win_width = 550;
    private static final int win_height = 480;

    private JTextArea content = new JTextArea(); // 聊天内容
    private JPanel sepPanel = new JPanel();
    private JTextArea chat = new JTextArea(); // 聊天输入框

    private JButton closeBtn = new JButton("关闭");
    private JButton sendBtn = new JButton("发送");

    private JList userList = new JList(); // 在线用户列表

    private Socket s = null;

    private JFrame _self = this;

    private String sendMsg = "";

    public ChatClient() {
        initWin();
        initBind();

        try {
            s = new Socket(common.getLocalIp(), 30000); //连接服务器
            new Thread(new ClientThread(s)).start(); // 处理服务端发过来的信息
            
        } catch (UnknownHostException e) {
            //e.printStackTrace();
        } catch (IOException e) {
            //e.printStackTrace();
            setContent("服务端没有开启。。。", 2);
        }

    }

    public void initBind() {
        closeBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        sendBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (chat.getText().trim().equals("")) {
                    JOptionPane.showMessageDialog(_self, "请输入内容");
                } else {
                    sendMsg = chat.getText().trim();
                    sendMsg(sendMsg);
                }
            }

        });
    }

    private void sendMsg(String sendMsg) {
        // 客户端发给服务端
        PrintStream ps;
        try {
            
            ps = new PrintStream(s.getOutputStream());
            ps.println(sendMsg);
            
            setContent(sendMsg, 1);
            chat.setText("");
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void setContent(String content, int flag){
        switch(flag){
            case 1:
                this.content.append("客户端发给服务器信息：" + content + "\r\n");
            break;
            
            case 2:
                this.content.append("服务器发过来的信息：" + content + "\r\n");
            break;
            
        }
        
    }

    class ClientThread implements Runnable {
        private Socket s = null;
        private BufferedReader br = null;

        public ClientThread(Socket s) {
            try {
                this.s = s;
                br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void run() {
            try {
                String content = null;
                while ((content = br.readLine()) != null) {
                    setContent(content, 2);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void initFillData() {
        // 获取在线用户

    }

    private void initWin() {
        JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false);

        JPanel left = new JPanel();
        JPanel right = new JPanel();

        split.setLeftComponent(left);
        split.setRightComponent(right);
        split.setDividerLocation(420);
        split.setDividerSize(8);

        left.setLayout(null);
        right.setLayout(null);

        content.setBounds(20, 20, 400, 300);
        content.setLineWrap(true);
        content.setWrapStyleWord(true);

        sepPanel.setBounds(20, 320, 400, 10);
        sepPanel.setBackground(Color.LIGHT_GRAY);

        chat.setBounds(20, 330, 400, 80);
        chat.setLineWrap(true);
        chat.setWrapStyleWord(true);

        closeBtn.setBounds(180, 420, 100, 25);
        sendBtn.setBounds(300, 420, 100, 25);

        left.add(content);
        left.add(sepPanel);
        left.add(chat);
        left.add(closeBtn);
        left.add(sendBtn);

        userList.setBackground(new Color(240, 240, 240));
        userList.setBounds(5, 10, 100, 400);

        right.add(userList);

        this.add(split);

        this.setTitle("局域网聊天");
        this.setSize(win_width, win_height);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        HashMap<String, Integer> d = common.getWindowCenterSize(win_width, win_height);
        this.setLocation(d.get("width"), d.get("height"));
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new ChatClient();
    }
}
